{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df_follower=pd.read_table(\"/nfs/maciej/twitter/Prediction_of_Viral_Memes_on_Twitter/follower_gcc.anony.dat\",header=None,delim_whitespace=True)\n",
    "df_follower.columns=['user1','user2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "84caf7584dc649de85f0bab2b0491b32",
       "version_major": 2,
       "version_minor": 0
      },
      "text/html": [
       "<p>Failed to display Jupyter Widget of type <code>HBox</code>.</p>\n",
       "<p>\n",
       "  If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n",
       "  that the widgets JavaScript is still loading. If this message persists, it\n",
       "  likely means that the widgets JavaScript library is either not installed or\n",
       "  not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
       "  Widgets Documentation</a> for setup instructions.\n",
       "</p>\n",
       "<p>\n",
       "  If you're reading this message in another notebook frontend (for example, a static\n",
       "  rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
       "  it may mean that your frontend doesn't currently support widgets.\n",
       "</p>\n"
      ],
      "text/plain": [
       "HBox(children=(IntProgress(value=1, bar_style='info', max=1), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm_notebook as tqdm\n",
    "import codecs\n",
    "\n",
    "dfs = []\n",
    "with open('/nfs/maciej/twitter/Prediction_of_Viral_Memes_on_Twitter/timeline_tag.anony.dat', 'rb') as f:\n",
    "    reader = codecs.getreader(\"utf-8\")\n",
    "    contents = reader(f)\n",
    "    for line in tqdm(contents):\n",
    "        split=line.split(\" \")\n",
    "        length=len(split)\n",
    "        for i in range(1,length-1):\n",
    "            split2=split[i].split(\",\")\n",
    "            dfs.append([split[0],split2[0],split2[1]])\n",
    "        split2=split[length-1].split(\",\")\n",
    "        dfs.append([split[0],split2[0],split2[1][:-1]])\n",
    "df_tag=pd.DataFrame(dfs)\n",
    "del dfs\n",
    "del split\n",
    "del split2\n",
    "del length\n",
    "df_tag.columns=['tag','ts','user']\n",
    "from collections import defaultdict\n",
    "d = defaultdict(lambda: len(d))\n",
    "df_tag['tagID'] = df_tag.apply(lambda row: d[row['tag']], axis=1)\n",
    "df_tag['ts']=df_tag['ts'].astype(int)\n",
    "df_tag['user']=df_tag['user'].astype(int)\n",
    "df_tag.sort_values(by=['ts'], axis=0, ascending=True, inplace=True, kind='quicksort', na_position='last')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_tag_unique=df_tag.drop_duplicates(subset=['tagID', 'user'], keep='first', inplace=False)[['tagID', 'user','ts']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0   0\n",
      "1   0\n",
      "1   -1508756236.923468\n",
      "2   -1508756236.923468\n",
      "2   -1508756237.112656\n",
      "2   -1508756237.457247\n",
      "2   -1508756237.9671648\n",
      "2   -1508756238.6506248\n",
      "3   -1508756238.6506248\n",
      "4   -1508756238.6506248\n",
      "5   -1508756238.6506248\n",
      "6   -1508756238.6506248\n",
      "7   -1508756238.6506248\n",
      "8   -1508756238.6506248\n",
      "9   -1508756238.6506248\n",
      "10   -1508756238.6506248\n",
      "11   -1508756238.6506248\n",
      "11   -1508756238.991366\n",
      "12   -1508756238.991366\n",
      "12   0.17162299156188965\n",
      "12   0.173553466796875\n",
      "12   -0.3402748107910156\n",
      "12   -1.0268301963806152\n",
      "12   -1.8734591007232666\n",
      "12   -0.5066347122192383\n",
      "12   -1.6538009643554688\n",
      "13   -1.6538009643554688\n",
      "13   0.16619634628295898\n",
      "14   0.16619634628295898\n",
      "15   0.16619634628295898\n",
      "16   0.16619634628295898\n",
      "16   -0.04675126075744629\n",
      "17   -0.04675126075744629\n",
      "18   -0.04675126075744629\n",
      "19   -0.04675126075744629\n",
      "20   -0.04675126075744629\n",
      "20   0.16732144355773926\n",
      "21   0.16732144355773926\n",
      "21   -0.016039609909057617\n",
      "22   -0.016039609909057617\n",
      "23   -0.016039609909057617\n",
      "24   -0.016039609909057617\n",
      "25   -0.016039609909057617\n",
      "26   -0.016039609909057617\n",
      "26   0.16468524932861328\n",
      "27   0.16468524932861328\n",
      "28   0.16468524932861328\n",
      "29   0.16468524932861328\n",
      "29   0.17003536224365234\n",
      "30   0.17003536224365234\n",
      "30   0.16498112678527832\n",
      "30   0.16916108131408691\n",
      "30   -0.3287479877471924\n",
      "30   0.16648650169372559\n",
      "30   0.1689155101776123\n",
      "30   0.16692185401916504\n",
      "30   -8.487701416015625e-05\n",
      "30   -7.772445678710938e-05\n",
      "30   -1.497065544128418\n",
      "30   0.16889286041259766\n",
      "30   -1.6573522090911865\n",
      "30   0.1704103946685791\n",
      "31   0.1704103946685791\n",
      "32   0.1704103946685791\n",
      "33   0.1704103946685791\n",
      "34   0.1704103946685791\n",
      "34   0.1726536750793457\n",
      "35   0.1726536750793457\n",
      "36   0.1726536750793457\n",
      "37   0.1726536750793457\n",
      "38   0.1726536750793457\n",
      "39   0.1726536750793457\n",
      "39   -0.07630085945129395\n",
      "39   -0.4055969715118408\n",
      "39   -0.9046194553375244\n",
      "39   -1.5726335048675537\n",
      "39   -2.4068760871887207\n",
      "39   0.17008519172668457\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-24-5169872d22f1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     18\u001b[0m             \u001b[0muser2\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mevent\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     19\u001b[0m             \u001b[0mstart\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mdf_follower\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mquery\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'(@user1 == user1 and @user2 == user2) or (@user1 == user2 and @user2 == user1)'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mempty\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     21\u001b[0m                 \u001b[0mend\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtime\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     22\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0mrow\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'ts'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0mevent\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36mquery\u001b[0;34m(self, expr, inplace, **kwargs)\u001b[0m\n\u001b[1;32m   2211\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2212\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2213\u001b[0;31m             \u001b[0mnew_data\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2214\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2215\u001b[0m             \u001b[0;31m# when res is multi-dimensional loc raises, but this is sometimes a\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   1310\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_tuple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1311\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1312\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1313\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1314\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_getitem_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   1453\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_slice_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1454\u001b[0m         \u001b[0;32melif\u001b[0m \u001b[0mis_bool_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1455\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getbool_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1456\u001b[0m         \u001b[0;32melif\u001b[0m \u001b[0mis_list_like_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1457\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getbool_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   1318\u001b[0m         \u001b[0mlabels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1319\u001b[0m         \u001b[0mkey\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcheck_bool_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlabels\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1320\u001b[0;31m         \u001b[0minds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnonzero\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1321\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1322\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtake\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconvert\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "start,end=0,0\n",
    "print(end - start)\n",
    "\n",
    "dict_A_u=defaultdict(lambda: 0)\n",
    "dict_A_v2u=defaultdict(lambda: 0)\n",
    "dict_A_viu=defaultdict(lambda: 0)\n",
    "\n",
    "for i in range(len(d)):\n",
    "    current_table=[]\n",
    "    for index, row in df_tag_unique[df_tag_unique['tagID']==i].iterrows():\n",
    "        \n",
    "        #print(row['user'])\n",
    "        user1=row['user']\n",
    "        dict_A_u[user1]=dict_A_u[user1]+1\n",
    "        parents=[]\n",
    "        for event in current_table:  \n",
    "            #print(type(event[0]))\n",
    "            user2=event[0]\n",
    "            start = time.time()\n",
    "            if not df_follower.query('(@user1 == user1 and @user2 == user2) or (@user1 == user2 and @user2 == user1)').empty: \n",
    "                end = time.time()    \n",
    "                if row['ts']>event[2]:\n",
    "                    dict_A_v2u[(user2,user1)]=dict_A_v2u[(user2,user1)]+1\n",
    "                dict_A_viu[(user2,user1)]=dict_A_viu[(user2,user1)]+1\n",
    "                dict_A_viu[(user1,user2)]=dict_A_viu[(user1,user2)]+1\n",
    "        print(i,' ',end - start)\n",
    "        current_table.append([user1,row['tagID'],row['ts']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_tag_unique.to_csv('actionsFile_v666',sep=' ',columns=['user','tagID','ts'],header=False,index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "54.6 ns ± 43 ns per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit -n10 pass\n",
    "\n",
    "l=[]\n",
    "for i in range(10000000):\n",
    "    l.append([i,i,i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000000"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_follower.query('(@user1 == user1 and @user2 == user2) or (@user1 == user2 and @user2 == user1)').empty"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
